package cn.kgc.music.controller;

import cn.kgc.music.common.RandomNumber;
import cn.kgc.music.pojo.User;
import cn.kgc.music.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {

    @Resource
    UserService userService;
    @RequestMapping("/login.do")

    public String login(String userName, String password,HttpServletRequest request){
        User user = userService.login(userName, password);
        if(user!=null){
            request.getSession().setAttribute("usr",user);
            return "index";
        }else{
            request.setAttribute("msg","用户名或密码错误");
            return "login";
        }
    }


    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @RequestMapping("/zhuce.do")
    public String zhuce(String phone,String password,HttpServletRequest request){
        int code=Integer.parseInt(RandomNumber.random());
        String nickName="用户"+code;
        User user=new User();
        user.setNickName(nickName);
        user.setPhone(phone);
        user.setPassword(password);
        int a=userService.adduser(user);
        if(a>0){
            request.getSession().setAttribute("user",user);
            return "index";
        }else{
            request.setAttribute("msg","注册失败");
            return "zhuce";
        }
    }
}
